package com.education.basic.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.education.basic.dao.TeachInfoDAO;
import com.education.basic.entity.TeachInfo;
import com.education.basic.resource.Courses;
import com.education.basic.resource.Major;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.education.basic.dao.ScoreDAO;
import com.education.basic.entity.Score;
import com.education.basic.service.ScoreService;
import com.yunshangwoniu.core.baseService.PageService;
import com.yunshangwoniu.core.page.Page;
import com.yunshangwoniu.core.utils.Logger;
import com.yunshangwoniu.core.utils.RequestUtils;
import org.springframework.transaction.annotation.Transactional;

@Service("scoreService")
public class ScoreServiceImpl extends BaseServiceImpl<Score> implements ScoreService
{
    @Autowired
    private ScoreDAO scoreDAO;
    @Autowired
	private PageService<Score> pageService;
    @Autowired
    private TeachInfoDAO teachInfoDAO;
	
	@Override
	public Page<Score> queryScore(Map<String,Object> params,int currentPageNum,int pageSize,HttpServletRequest request) 
	{
		Page<Score> page = new Page<Score>(currentPageNum, pageSize,RequestUtils.getPageUrl(request));
		page.setParams(params);
		page = scoreDAO.query(page, Score.class);
		pageService.htmlBuilder(page, "default");
		try
		{
			Logger.debug(this.getClass(), String.format("get Score success  size is %s ", page.getRecords().size()));
			List<Score> scores = page.getRecords();
			sortScore(scores,0,scores.size());						//分数排名
		}catch(Exception e)
		{
			Logger.debug(this.getClass(), String.format("get Score failed"));
		}
		return page;
	}
	
	private void sortScore(List<Score> list,int first,int last)
	{
		if(list!=null&&list.size()>0)
		{
			int left = first;
			int right = last;
			while(left!=right)
			{
				while(list.get(left).getScore()>list.get(right).getScore())
				{
					right--;
				}
				Score temp = list.get(right);
				list.add(left,list.get(right));
				list.add(right,temp);
				while(list.get(right).getScore()<list.get(left).getScore())
				{
					left++;
				}
				temp = list.get(right);
				list.add(left,list.get(right));
				list.add(right,temp);
			}
			sortScore(list,first,left);
			sortScore(list,left,last);
		}
	}

    @Override
    @Transactional
    public Map<String, List<Courses>> findCourseTree(String empno)
    {
        List<TeachInfo> infos = teachInfoDAO.getTeachInfosByEmpno(empno);
        Map<String, List<Courses>> trees = new HashMap<String,  List<Courses>>();
        if(infos!=null&&infos.size()>0)
        {
            for(TeachInfo info : infos)
            {
                String courseCode = info.getCourseCode();
                Courses courses = Courses.findByCode(courseCode);
                Major major = courses.getMajor();
                String majorName = major.getName();
                if(trees.containsKey(majorName))
                {
                    List<Courses> list = trees.get(majorName);
                    list.add(courses);
                }else
                {
                    List<Courses> list = new ArrayList<Courses>();
                    list.add(courses);
                    trees.put(majorName,list);
                }
            }
        }
        return trees;
    }

    @Override
    @Transactional
    public String fillScore(long id, Double score)
    {

        Score score1 = scoreDAO.getById(Score.class,id);
        if(score1==null)
        {
            Logger.debug(this.getClass(),String.format("update score failed where [id:%s]",id));
            return "该条记录不存在";
        }else
        {
            score1.setScore(score);
            scoreDAO.update(score1);
            Logger.debug(this.getClass(),String.format("update score success!! where [id:%s]",id));
            return "操作成功";
        }
    }
}
